<!-- 附件信息 -->
<template>
  <div class="materials-card attachment-info">
    <div class="p-title mb-12">附件信息</div>

    <el-upload
      v-if="!props.isView"
      drag
      :limit="10"
      action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
      multiple
    >
      <el-icon class="el-icon--upload" color="#C0C4CC" size="48"><upload-filled /></el-icon>
      <div class="el-upload__text ext-grey">将文件拖拽至此或 <em>点击上传</em></div>
      <div class="el-upload__text ext-grey">支持批量上传文件，文件格式不限，最多只能上传 10 份文件</div>
    </el-upload>

    <div class="flx-justify-between selected-header">
      <div class="text">已选<span class="text-primary">2</span>份文件</div>
      <el-button type="primary" plain>全部下载</el-button>
    </div>
    <div class="file-list">
      <div class="file-item flx-justify-between" v-for="(item, index) in fileList" :key="index">
        <div class="left flx-align-center">
          <el-checkbox></el-checkbox>
          <el-icon size="16" color="#909399"><Document /></el-icon>
          <template v-if="!item.isEdit">
            <span class="text">{{ item.name }}</span>
            <el-icon v-if="!props.isView" class="pointer" size="16" color="#3370FF" @click="item.isEdit = true"
              ><EditPen
            /></el-icon>
          </template>
          <template v-if="item.isEdit">
            <el-input v-model="item.name"></el-input>
            <el-button link type="primary" @click="saveRow(item)">保存</el-button>
            <el-button link @click="item.isEdit = false">取消</el-button>
          </template>
        </div>
        <div class="right" :class="{ right2: $props.isView }">
          <el-button link type="primary" @click="viewsImg(item)">查看</el-button>
          <el-button link type="primary">下载</el-button>
          <el-button link type="primary" v-if="!$props.isView">裁剪</el-button>
        </div>
      </div>
    </div>

    <el-image-viewer v-if="imgViewVisible" :url-list="[viewImageUrl]" @close="imgViewVisible = false" />
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps } from "vue";

const props = defineProps({
  isView: {
    type: Boolean,
    default: false
  }
});

const fileList = ref([{ name: "图片.jpg", isEdit: false }]);

// 图片预览
const imgViewVisible = ref(false);
const viewImageUrl = ref("");
const viewsImg = row => {
  imgViewVisible.value = true;
  viewImageUrl.value = row.url;
};

// 修改文件名
const saveRow = item => {
  item.isEdit = false;
};
</script>

<style lang="scss">
.attachment-info {
  .el-upload-dragger {
    .el-icon--upload {
      color: #c0c4cc;
    }
    .el-upload__text {
      font-size: 12px;
      color: #909399;
      line-height: 20px;
    }
  }

  .selected-header {
    margin-bottom: 4px;
    .text {
      font-size: 13px;
      color: #606266;
      height: 22px;
    }
  }

  .file-list {
    .file-item {
      margin-top: 4px;
      background: #ffffff;
      padding: 0 8px;
      height: 32px;

      .left {
        font-size: 13px;
        flex: 1;

        .text {
          vertical-align: middle;
          margin-top: -4px;
        }

        i {
          padding: 0 4px;
        }

        .el-input {
          width: 154px;
          height: 24px;
        }

        .el-button {
          margin-left: 4px;
        }
      }

      .right {
        width: 180px;
        display: flex;

        &.right2 {
          width: 120px;
        }

        .el-button {
          width: 60px;
          margin-left: 0;
        }
      }
    }
  }
}
</style>
